angular.module('starter.services', [])

    .constant("ENV", {
        // deviceID: localStorage.pjqNum,
        // serverIP: localStorage.ip,
        // serverPort: localStorage.port,
        // serverPath: "http://" + localStorage.ip + ":" + localStorage.port
    })
    .factory('Storage', function () {
        "use strict";
        return {
            set: function (key, data) {
                return window.localStorage.setItem(key, window.JSON.stringify(data));
            },
            get: function (key) {
                return window.localStorage.getItem(key);
            },
            remove: function (key) {
                return window.localStorage.removeItem(key);
            }
        };
    })
    .service('ConfigService', ['$http', '$q', 'Storage', function ($http, $q, Storage) {
        var config = Storage.get("config");
        config = JSON.parse(config);
        console.log(config);
        var hostURL = "";
        if (config) {
            hostURL = "http://" + config.ip + ":" + config.port;
            console.log(hostURL);
        }
        // var hostURL = "";
        var service = {
            //用于统一设置url
            getHost: function () {
                return hostURL;
            },
            getCurrentUser: function () {
                var userInfo = Storage.get("userInfo");
                return userInfo;
            },
            testConnectServer: function (ip, port, pjqNum) {
                var deferred = $q.defer();
                var url = "http://" + ip + ":" + port + "/ws/testConnectServer?pjqNum=" + pjqNum;
                console.log(url);
                $http({
                    method: 'GET',
                    url: url
                }).success(function (data, status, header, config) {
                    deferred.resolve(data);
                }).error(function (data, status) {
                    deferred.resolve(data);
                });
                return deferred.promise;
            }
        }
        return service;
    }])
    .service('AppService', ['$http', '$q', 'Storage', 'ConfigService', function ($http, $q, Storage, ConfigService) {
        return {
            testConfig: function () {
                return "no";
            },
            init: function (pjqNum, latestTime) {
                var deferred = $q.defer();
                var url = ConfigService.getHost() + "/ws/init";
                console.log(url);
                var pjqNum = Storage.get("config").pjqNum;
                var latestTime = "2017-08-28 07:07:07";
                $http({
                    method: 'POST',
                    url: url,
                    data: {"pjqNum": pjqNum, "latestTime": latestTime}
                }).success(function (data, status, header, config) {
                    deferred.resolve(data);
                }).error(function (data, status) {
                    deferred.resolve(data);
                });
                return deferred.promise;
            },
            getMessage: function () {
                var deferred = $q.defer();
                var url = ConfigService.getHost() + "/pjqPad/getUserinfo";
                console.log(url);
                $http({
                    method: 'POSST',
                    url: url,
                    data: {"pjqNum": localStorage.pjqNum}
                }).success(function (data, status, header, config) {
                    deferred.resolve(data);
                }).error(function (data, status) {
                    deferred.resolve(data);
                });
                return deferred.promise;
            },
            findNoSubmitRec: function (jobId) {
                var deferred = $q.defer();
                var url = ConfigService.getHost() + "/app/job/findNoSubmitRec?jobId=" + jobId;
                console.log(url);
                $http({
                    method: 'GET',
                    url: url
                }).success(function (data, status, header, config) {
                    deferred.resolve(data);
                });
                return deferred.promise;
            }
        }
    }])
    .service('HomeService', ['$http', '$q', 'Storage', 'ConfigService', function ($http, $q, Storage, ConfigService) {
        return {
            getUserInfo: function () {
                var deferred = $q.defer();
                var url = ConfigService.getHost() + "/ws/getUserInfo";
                console.log(url);
                var pjqNum = Storage.get("config").pjqNum;
                $http({
                    method: 'POST',
                    url: url,
                    data: {"pjqNum": pjqNum}
                }).success(function (data, status, header, config) {
                    deferred.resolve(data);
                }).error(function (data, status) {
                    deferred.resolve(data);
                });
                return deferred.promise;
            },
        }
    }])

